//
//  GJFUnderstandViewController.m
//  Wedding
//
//  Created by 郭静芬 on 2017/1/9.
//  Copyright © 2017年 郭静芬. All rights reserved.
//

#import "GJFCaseDetailViewController.h"
#import "GJFTitleTableViewCell.h"
#import "GJFMerchantDetailTagTableViewCell.h"
#import "GJFMerchantDetailCaseTableViewCell.h"
#import "GJFReferenceTableViewCell.h"
#import "GJFMerchantDetailIntroduceTableViewCell.h"
#import "GJFInspirationModel.h"

@interface GJFCaseDetailViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) GJFInspirationModel *modelCase;
@property (nonatomic, strong)UILabel *caseName;

@end

@implementation GJFCaseDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self getCaseDetailData];
    [self tableView];
}
- (void)getCaseDetailData {
    NSDictionary *dict = @{@"uuid":self.caseDetailModel.uuid?:@""};
    [GJFNetworkTool POSTRequestWithUrl:APP_INSPIRATION_DETAIL params:dict complete:^(NSDictionary *result) {
        if ([result[@"code"] integerValue] == 1000) {
            NSDictionary *dic = result[@"data"];
            self.modelCase = [GJFInspirationModel  mj_objectWithKeyValues:dic];
            [self.tableView reloadData];
        }
    }];
}

#pragma mark - TableView的分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}
#pragma mark - TableView每个分区中row数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 2;
    }
    else if (section == 1) {
        return 1;
    }
    else {
        return 2;
    }
}
#pragma mark - TableView每个分区中row的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            GJFTitleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TitleCell"];
            cell.caseDetailTitleModel = self.modelCase;
            return cell;
        }
        else {
            GJFMerchantDetailTagTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TagCell"];
            cell.merchantModel = self.modelCase.merchant;
            return cell;
        }
    }
    else if (indexPath.section == 1) {
        GJFMerchantDetailCaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CaseCell"];
        cell.caseModel = self.modelCase.merchant.myCase[indexPath.row];
        return cell;
    }
    else {
        if (indexPath.row == 0) {
            GJFReferenceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ReferenceCell"];
            cell.referenceModel = self.modelCase;
            return cell;
        }
        else {
            GJFMerchantDetailIntroduceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"IntroduceCell"];
            cell.merchantModel = self.modelCase.merchant;
            cell.introduceDetail.font = [UIFont systemFontOfSize:16 * XX];
            return cell;
        }
    }
}
#pragma mark - TableView中cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return 70;
        }
        else {
            NSArray *array = [self.modelCase.merchant.taglist componentsSeparatedByString:@","];
            if (array.count > 4) {
                return 120;
            }
            return 80;
        }
    }
    else if (indexPath.section == 1){
        return 135;
    }
    else {
        if (indexPath.row == 0) {
            return 95;
        }
        else {
            CGFloat h = [GJFSingleTool heightWithText:self.modelCase.merchant.introduction fontSize:18 Width:WIDTH - 20];
            return h + 40;
        }
    }
}
#pragma mark - TableView每个分区头部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        return 35;
    }
    else {
        return 0.1;
    }
}
#pragma mark - TableView每个分区尾部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}
#pragma mark - TableView二个分区头部的内容
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        UIView *header_view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 30)];
        [header_view addSubview:self.caseName];
        return header_view;
    }
    return nil;
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellEditingStyleNone;
        
        [_tableView registerClass:[GJFTitleTableViewCell class] forCellReuseIdentifier:@"TitleCell"];
        [_tableView registerClass:[GJFMerchantDetailTagTableViewCell class] forCellReuseIdentifier:@"TagCell"];
        [_tableView registerClass:[GJFMerchantDetailCaseTableViewCell class] forCellReuseIdentifier:@"CaseCell"];
        [_tableView registerClass:[GJFReferenceTableViewCell class] forCellReuseIdentifier:@"ReferenceCell"];
        [_tableView registerClass:[GJFMerchantDetailIntroduceTableViewCell class] forCellReuseIdentifier:@"IntroduceCell"];

        [self.view addSubview: _tableView];
    }
    return _tableView;
}
- (UILabel *)caseName {
    if (!_caseName) {
        _caseName = [[UILabel alloc] initWithFrame:CGRectMake(10, 15, WIDTH - 20, 30)];
        _caseName.text = @"案例来源";
        _caseName.textAlignment = NSTextAlignmentCenter;
        _caseName.textColor = HOMECOLOR;
        _caseName.font = [UIFont systemFontOfSize:19 * XX];
    }
    return _caseName;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
